package com.twu.biblioteca.models;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ema93sh
 * Date: 8/9/13
 * Time: 4:18 PM
 */
public class Library {
    private List<Book> bookCollection;
    private List<Movie> moviesCollection;

    public Library()
    {
       bookCollection = new ArrayList<Book>();
       moviesCollection = new ArrayList<Movie>();
    }

    public boolean addBook(Book book) {

        if (!bookCollection.contains(book)) {
            bookCollection.add(book);
            return true;
        } else {
            return false;
        }
    }

    public Book[] getAllBooks() {
        Book[] books = new Book[bookCollection.size()];
        bookCollection.toArray(books);
        return books;
    }

    public boolean reserve(Book book) {        // return true on success
        if (book == null) return false;
        if (book.IsReserved())
            return false;
        book.reserve();
        return true;
    }

    public boolean reserve(String title)
    {
        Book book = getBook(title);
        return reserve(book);
    }

    public boolean reserve(int id)
    {
        Book book = getBook(id);
        return reserve(book);
    }

    public Book getBook(int id) {
        if ( id <= 0 ) return null;
        if ( id > bookCollection.size()) return null;
        return bookCollection.get(id-1);
    }

    public Book getBook(String title) {
        for ( Book book: bookCollection)
        {
            if (book.getTitle().equalsIgnoreCase(title))
            {
                return book;
            }
        }
        return null;
    }

    public Movie getMovie(int id) {
        return moviesCollection.get(id-1);
    }

    public void addMovie(Movie movie) {
        if (!moviesCollection.contains(movie))
            moviesCollection.add(movie);
    }

    public Movie[] getAllMovies() {
        Movie[] movies = new Movie[moviesCollection.size()];
        moviesCollection.toArray(movies);
        return movies;
    }
}
